def cv():
    print("cv is do")

emenu=[
    {"name":"m1","type":"def","def":"cv()","text":False,"menu":False,"logo":False},
    {"name":"m2","type":"text","def":False,"text":"This is test","menu":False,"logo":False},
    {"name":"m3","type":"menu","def":False,"text":False,
     "menu":[
        {"name":"m3-1","type":"def","def":"cv()","text":False,"menu":False,"logo":False},
        {"name":"m3-2","type":"def","def":"cv()","text":False,"menu":False,"logo":False},
        {"name":"m3-3","type":"def","def":"cv()","text":False,"menu":False,"logo":False},      
        ],
     "logo":False},
    {"name":"m4","type":"def","def":"cv()","text":False,"menu":False,"logo":False},
    {"name":"m5","type":"def","def":"cv()","text":False,"menu":False,"logo":False},
    {"name":"m6","type":"def","def":"cv()","text":False,"menu":False,"logo":False},
]
# eval()
#执行函数
#def menu_do(menu,show=4,action=0):

class Menu(object):
    def __init__(self,one_menu):
        self.menu_now=one_menu
        self.menu_list=[]
        self.menu_list.append(self.menu_now)
        self.select=0
    # 显示函数
    def text(self,n):
        print(n)        
    def menu_show(self,li,one):
        k=0
        for i in li:
            if k==one:
                print("* ",i['name'])
            else:
                print("  ",i['name'])
            k=k+1
    # 显示函数
    def menu_select(self,num=0,show=4):
        menu=self.menu_now
        z=len(menu)
        if num<0:
            num=0
        if num>=z-1:
            num=z-1
        self.select=num
        return self.menu_show(menu[4*(num//show):4*(num//show)+4],num%show)
    def menu_action(self):
        num=self.select
        menu=self.menu_now
        if menu[num]['type']=="def":
            eval(menu[num]['def'])
#             执行函数命令，需要手动返回
        elif menu[num]['type']=="text":
            self.text(menu[num]['text'])
#             执行显示文本命令，需要手动返回
#             return menu
        elif menu[num]['type']=="menu":
            self.menu_now=menu[num]['menu']
            self.menu_list.append(menu[num]['menu'])
            self.menu_select()
            #默认进入下子菜单
#             return menu[num]['menu']
    def menu_back(self):
        #菜单返回
        if len(self.menu_list)==1:
            self.menu_select()
        else:
            self.menu_list.pop()
            self.menu_now=self.menu_list[-1]
            self.menu_select()
    def select_add(self):
        num=self.select+1
        self.menu_select(num,4)
    def select_dec(self):
        num=self.select-1
        self.menu_select(num,4)
    def select_now(self):
        num=self.select
        self.menu_select(num,4)
    def select_back(self):
        self.menu_back()
    def select_action(self):
        self.menu_action()
c=Menu(emenu)
c.menu_select(3)
c.menu_back()
